// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
#include "pch.h"

using namespace std;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::UI::Core;

namespace Weathr
{
    // The run_async_non_interactive function allows lower priority (noninteractive) work to be scheduled on the main thread.
    void run_async_non_interactive(function<void ()> && action)
    {
        CoreWindow^ wnd = CoreApplication::MainView->CoreWindow;
        assert(wnd != nullptr);

        wnd->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([action]()
        {
            action();
        }));
    }
}

#ifndef NDEBUG
unsigned int _mainThreadId = 0U;

#ifdef  __cplusplus
extern "C" {
#endif

    // The IsMainThread function returns true if the current thread is the app's main thread and false otherwise.
    bool IsMainThread()
    {
        return (_mainThreadId == 0U || _mainThreadId == GetCurrentThreadId());
    }

    // The IsBackgroundThread function returns false if the current thread is the app's main thread and true otherwise.
    bool IsBackgroundThread()
    {
        return (_mainThreadId == 0U || _mainThreadId != GetCurrentThreadId());
    }

    // The RecordMainThread function registers the main thread ID for use by the IsMainThread and IsBackgroundThread functions.
    void RecordMainThread()
    {
        _mainThreadId = GetCurrentThreadId();
    }

#ifdef  __cplusplus
}
#endif

#endif /* not NDEBUG */